/*
    SUSAN® - Sole of Unix Save ANything

   

   
*/

#ifndef BAREOS_LIB_BNET_NETWORK_DUMP_PRIVATE_H_
#define BAREOS_LIB_BNET_NETWORK_DUMP_PRIVATE_H_

#include "lib/bareos_resource.h"

#include <string>
#include <memory>
#include <set>
#include <fstream>

class QualifiedResourceNameTypeConverter;
class BStringList;

class BnetDumpPrivate {
 public:
  BnetDumpPrivate() = default;
  ~BnetDumpPrivate() = default;

  static bool SetFilename(const char* filename);

  void DumpToFile(const char* ptr, int nbytes);
  void SaveAndSendMessageIfNoDestinationDefined(const char* ptr, int nbytes);
  void OpenFile();
  void CloseFile();

  static std::string filename_;
  static bool plantuml_mode_;
  static int stack_level_amount_;

  std::string own_qualified_name_;
  std::string destination_qualified_name_;

  std::ofstream output_file_;

 private:
  void CreateAndWriteStacktraceToBuffer();
  void CreateAndWriteMessageToBuffer(const char* ptr, int nbytes);
  std::string CreateDataString(int signal, const char* ptr, int nbytes) const;
  std::string CreateFormatStringForNetworkMessage(int signal) const;
  bool SuppressMessageIfRcodeIsInExcludeList() const;
  bool IsExcludedRcode(const BStringList& l) const;

  static int stack_level_start_;
  static std::size_t max_data_dump_bytes_;
  static std::set<std::string> exclude_rcodes_;

  std::string output_buffer_;

  std::vector<std::vector<char>> temporary_buffer_for_initial_messages_;

  enum class State
  {
    kWaitForDestinationName,
    kRunNormal
  };
  State state_ = State::kWaitForDestinationName;
};

#endif  // BAREOS_LIB_BNET_NETWORK_DUMP_PRIVATE_H_
